#include <glf.h>
#include <glfwindow.h>
#include <glfu.h>

static void initialise(void* userdata)
{
	int width = glfGetWidth();
	int height = glfGetHeight();
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1,1,1,1);
	glScissor(0,0,width/2,height/2);
	glEnable( GL_SCISSOR_TEST );
}

static void terminate(void* userdata)
{
}

static void think(void* userdata)
{
}

static void render(void* userdata)
{
	glClear(GL_COLOR_BUFFER_BIT);
}

static void resize(void* userdata)
{
	int width = glfGetWidth();
	int height = glfGetHeight();
	glViewport(0,0,width,height);
	glScissor(0,0,width,height);
	glClearColor(0,0,0,0);
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1,1,1,1);
	glScissor(0,0,width/2,height/2);
}

int main(void)
{

	glfConfigSetAttribute(glfMajorVersion, 3);
	glfConfigSetAttribute(glfMinorVersion, 1);
	glfConfigSetAttribute(glfContextFlags, glfContextForwardCompatible);
	glfConfigSetAttribute(glfSizable, 1 );

	if( !glfCreateWindow( "Scissor test", 640, 480 ) )
	{
		return -1;
	}
	
	glfSetCallBack(glfCallbackInitialise, initialise, NULL );
	glfSetCallBack(glfCallbackThink, think, NULL );
	glfSetCallBack(glfCallbackRender, render, NULL );
	glfSetCallBack(glfCallbackResize, resize, NULL );
	glfSetCallBack(glfCallbackTerminate, terminate, NULL );

	if( !glfMainloop() )
	{
		return -1;
	}

	glfDestroyWindow();

	return 0;
}
